<?php

defined('BASEPATH') or die('forbidden');

class noticemamage_Controller Extends Controller {

    private $platform;
    private $appid;
    private $type;
    private $language;
    private $apptype;

    function _init() {
        check_level(50, segment(0), segment(1), segment(2, 'index'));
        $this->platform=array('IOS'=>'IOS','Android'=>'Android','Win32'=>'Win32');
        $this->channel = array('All'=>'All','AppStore' => 'AppStore', '91' => '91', 'PP' => 'PP','aiwangyou'=>'aiwangyou','uc'=>'uc' , 'kuaiyong' => 'kuaiyong' , 'itools' => 'itools' , "duoku" => "duoku" , "dangle" => "dangle" );
        $this->appid = array('5' => translate('NOTICE_APPID5'));
        $this->type = array('1' => translate('TYPE1'), '2' => translate('TYPE2'), '3' => translate('TYPE3'), '4' => translate('TYPE4'));//1、游戏内公告 2、登入公告 3、活动内容 4、弹窗公告
        $this->language = array('1' => translate('LANGUAGE1'), '2' => translate('LANGUAGE2'), '3' => translate('LANGUAGE3'));
        $this->apptype = array('1' => 'iphone960', '2' => 'ipad', '3' => 'iphone1136');
    }

    function index() {
        $this->view->display("notice/noticemamage/index");
    }

    function flexigrid() {
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
        $rows = array();
        $sql = "SELECT COUNT(id) AS num FROM announcement WHERE {$where}";
        $one = $this->db->one($sql);
        $total = $one['num'];
        $orderby = $sortname ? "ORDER BY {$sortname} {$sortorder} " : "";
        $sql = "SELECT {$colsinfo} FROM announcement WHERE {$where} {$orderby} LIMIT {$offset},{$rp}";
        $this->db->query($sql);
        $areaArray = get_area_list_host();
        while ($n = $this->db->get_array('default')) {
            $n['create_time'] = get_datetime($n['create_time']);
            $n['last_update_time'] = get_datetime($n['last_update_time']);
            $n['language'] = $this->language[$n['language']];
            $n['appid'] = $this->appid[$n['appid']];
            $n['platform'] = $this->platform[$n['platform']];
            $n['type'] = $this->type[$n['type']];
            //$n['contents'] = substr($n['contents'], 0,50);
            $rows[] = array('id' => $n['id'], 'cell' => ($n));
        }
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $rows
        );
        echo json_encode($result);
    }

    private function handleContents($contents) {
        if (!get_magic_quotes_gpc())
            $contents = addslashes($contents);
        return $contents;
    }

    private function creatfile($filename,$contents) {
        $handle = @fopen(ROOTPATH . "notice/" . $filename, "w+");
        fwrite($handle, $contents);
        fclose($handle);
    }

    function add() {
        if (is_submit()) {
            $rowdata = $_POST['row'];

            $rowdata['contents'] = $this->handleContents($rowdata['contents']);
            $rowdata['create_time'] = time();
            $rowdata['last_update_time'] = time();
            $sql = "select id from announcement where appid={$rowdata['appid']} and platform='".$rowdata['platform']."' 
                                                      and channel='".$rowdata['channel']."' and `type`=".$rowdata['type']." and 
                                                      language={$rowdata['language']} and servernum={$rowdata['servernum']} ";
            $one = $this->db->one($sql);
            $total = $one['id'];
            if (isset($total)) {
                $result = translate('NOTICE_ISSET');
                error($result);
            }
            $result = $this->db->insert('announcement', $rowdata);
            if ($result) {
                $curl_post = http_build_query($rowdata);
                $config = get_config();
                //创建广告文件
                $ret = vpost($config['host_url_announcement']."?act=add",5,$curl_post);
//                if ($rowdata['type'] == '4') {
//                    $filename = 'notice' . $rowdata['platform'] . '_' . $rowdata['appid'] . '.php';
//                    $this->creatfile($filename,time());
//                }
//                else {
//                    $filename = 'notice_cache/'.'appid='.$rowdata['appid'].'&platform='.$rowdata['platform'].'&channel='.$rowdata['channel'].'&language='.$rowdata['language'].'&type='.$rowdata['type'].'&servernum='.$rowdata['servernum'].'.ini';
//                    $this->creatfile($filename,$rowdata['contents']);
//                }
                insert_log(translate('MENU_NOTICE_NOTICEMAMAGE'),translate('ADD'), $rowdata);
                success(translate('C_TIPS_SUCCESS_TEXT'));
            } else {
                $result = translate('C_TIPS_ERROR_TEXT');
                error($result);
            }
        }

        $this->view->assign("appid", $this->form->select("row[appid]", $this->appid, $this->appid[5], array('id' => 'appid')));
        $this->view->assign("platform", $this->form->select("row[platform]", $this->platform, '', array('id' => 'platform')));
        $this->view->assign("channel", $this->form->select("row[channel]", $this->channel, '', array('id' => 'channel')));
        $this->view->assign('language', $this->form->select('row[language]', $this->language, '', array('id' => 'language')));
        $this->view->assign('type', $this->form->select('row[type]', $this->type, '', array('id' => 'type')));

        $this->view->display("notice/noticemamage/add");
    }

    function edit($id) {
        if (is_submit()) {
            $rowdata = $_POST['row'];
            $rowdata['last_update_time'] = time();
            $result = $this->db->update('announcement', $rowdata, array('id' => $id));
            if ($result) {
                $one = $this->db->one("select type,appid,platform,channel,language,servernum from announcement where id=$id");
                $one['contents'] = $rowdata['contents'];
                $config = get_config();

                $curl_post = http_build_query($one);
                $ret = vpost($config['host_url_announcement']."?act=add",5,$curl_post);
//                if ($one['type'] == '4') {
//                    $filename = 'notice' . $one['platform'] . '_' . $one['appid'] . '.php';
//                    $this->creatfile($filename,time());
//                }
//                else {
//                    $filename = 'notice_cache/'.'appid='.$one['appid'].'&platform='.$one['platform'].'&channel='.$one['channel'].'&language='.$one['language'].'&type='.$one['type'].'&servernum='.$one['servernum'].'.ini';
//                    $this->creatfile($filename,$rowdata['contents']);
//                }
                insert_log(translate('MENU_NOTICE_NOTICEMAMAGE'),translate('EDIT'), $one);
                success(translate('C_TIPS_SUCCESS_TEXT'));
            } else {
                $result = translate('C_TIPS_ERROR_TEXT');
                error($result);
            }
        }
        $sql = "select appid,platform,contents,servernum,type,language,channel from announcement where id=$id";
        $row = $this->db->one($sql);
        unset($arr);
        foreach ($row as $key => $value) {
            $arr[$key] = $value;
        }
       
        $this->view->assign("appid", $this->form->select("row[appid]", $this->appid, array('appid' => $arr['appid']), array('id' => 'appid', 'disabled' => 'disabled')));
        $this->view->assign("platform", $this->form->select("row[platform]", $this->platform, array('platform' => $arr['platform']), array('id' => 'platform', 'disabled' => 'disabled')));
        $this->view->assign("channel", $this->form->select("row[channel]", $this->channel, array('channel' => $arr['channel']),  array('id' => 'channel', 'disabled' => 'disabled')));
        $this->view->assign('language', $this->form->select('row[language]', $this->language, array('language' => $arr['language']), array('id' => 'language', 'disabled' => 'disabled')));
        $this->view->assign('type', $this->form->select('row[type]', $this->type, array('type' => $arr['type']), array('id' => 'type', 'disabled' => 'disabled')));
        $this->view->assign('servernum', $arr['servernum']);
        $this->view->assign('contents', $arr['contents']);
        $this->view->display("notice/noticemamage/edit");
    }

    function del($id) {
        $c = 0;
        $r = '';
        $one = $this->db->one("select type,appid,platform,channel,language,servernum from announcement where id=$id");
        $config = get_config();
        $ret = vpost($config['host_url_announcement']."?act=del",5,http_build_query($one));
        $where = array('id' => $id);
        if ($this->db->delete('announcement', $where)) {
            insert_log(translate('MENU_NOTICE_NOTICEMAMAGE'),translate('DEL'), array('id'=>$id));
            $c = 1;
            $r = translate('C_TIPS_SUCCESS_TEXT');
            
        } else {
            $r = translate('C_TIPS_ERROR_TEXT');
        }
        json_output($c, $r);
    }

    function multi() {
        $type = $this->input->post("type");
        $ids = $this->input->post("ids");
        $c = 0;
        $r = '';
        if (count(explode(',', $ids)) == 0) {
            $r = translate('C_INVALID_PARAMS');
        } else {
            if ($type == 'delete') {
                $this->db->query("DELETE FROM announcement WHERE id IN ({$ids})");
                insert_log(translate('MENU_NOTICE_NOTICEMAMAGE'),translate('MULTI_DEL'),array('ids'=> $ids));
                $c = 1;
            } else {
                $r = translate('C_INVALID_PARAMS');
            }
        }
        json_output($c, $r);
    }

}